var url_orderList = BASE_PATH + "getCustByAgent.web";

function loadOrderList(data, call) {
	formSubmitForAjax({
		url : url_orderList,
		data : data,
		success : call || showOrder,
		error : function(){
			try {
				removeLoadHtml();
				onSwiper();
				// mySwiper.setWrapperTranslate(0, 0, 0)
				mySwiper.params.onlyExternal = false;
				// Update active slide
				mySwiper.updateActiveSlide(0);
				loadingState = 1;
			} catch (e) {
				// TODO: handle exception
			}
		}
	});
}
function getOrderListView() {
	return $("#orderListView");
}
function getOrderBoxHeight() {
	return $('.swiper-container').height();
}
var loadingState = 1;
var holdPosition = 0;
var mySwiper;
function onSwiper() {
	try {
		var $orderListView = getOrderListView();
		var $lastOrder = $(".orderView:last", $orderListView);
		var startY = $lastOrder.offset().top;
		mySwiper = new Swiper('.swiper-container', {
			slidesPerView : 'auto',
			mode : 'vertical',
			watchActiveIndex : true,
			onTouchStart : function() {
				holdPosition = 0;
				startY = $lastOrder.offset().top;
			},
			onResistanceBefore : function(s, pos) {
				holdPosition = pos;
				// alert(pos);
			},
			onTouchEnd : function() {
				if(loadingState == 0){
					return false;
				}
				var Y = $lastOrder.offset().top;
				// 首先：说明是有上拉操作
				// alert(startY + ' - ' + getOrderBoxHeight());
				if (startY < (getOrderBoxHeight()) && (startY - Y) > 50) {
					// //其次 判断是否达到底部并需要加载
					// alert(getOrderBoxHeight() - Y);
					if (getOrderBoxHeight() - Y) {
						loadNexPageOrder();
					}
				} else if (holdPosition > 50) {
					// mySwiper.setWrapperTranslate(0,40,0)
					// mySwiper.params.onlyExternal=true
					// $('.preloader').addClass('visible');
					loadNewOrder();
				}
			}
		});
	} catch (e) {
		// TODO: handle exception
	}
}
var slideNumber = 0;
function loadNewOrder() {
	var data = {};
	data['PageNum'] = 1;
	data['BUY_TYPE'] = BUY_TYPE;
	mySwiper.params.onlyExternal = true;
	addLoadUp();
	loadingState = 0;
	loadOrderList(data, function(json) {
		removeLoadHtml();
		showOrder(json,0);
		onSwiper();
		// mySwiper.setWrapperTranslate(0, 0, 0)
		mySwiper.params.onlyExternal = false;
		// Update active slide
		mySwiper.updateActiveSlide(0);
		loadingState = 1;
		// Hide loader
		// $('#loadNex').removeClass('visible');
		slideNumber++;
	});
}
function loadNexPageOrder() {
	var data = {};
	data['PageNum'] = page + 1;
	data['BUY_TYPE'] = BUY_TYPE;
	// alert(page+STARTDATE+ENDDATE);

	// mySwiper.setWrapperTranslate(0, -40, 0)
	// mySwiper.params.onlyExternal = true;
	mySwiper.params.onlyExternal = true;
	// $('#loadNex').addClass('visible');

	addLoadNex();
	loadingState = 0;

	loadOrderList(data, function(json) {
		removeLoadHtml();
		showOrder(json,1);
		page++;
		onSwiper();
		// mySwiper.setWrapperTranslate(0, 0, 0)
		mySwiper.params.onlyExternal = false;
		// Update active slide
		mySwiper.updateActiveSlide(0);
		loadingState = 1;
		// Hide loader
		// $('#loadNex').removeClass('visible');
		slideNumber++;
	});
}

function addLoadUp() {
	removeLoadHtml();
	// var $box = getOrderListView();
	// $box.prepend(getLoadHtml());
	$('#loadRefresh').show();
}

function addLoadNex() {
	removeLoadHtml();
	// var $box = getOrderListView();
	// $box.append(getLoadHtml());
	$('#loadNex').show();

}

function getLoadHtml() {
	return '<div class="col-sm-8 col-xs-12 col-sm-offset-2 swiper-slide loadOrder">Loading...</div>';
}

function removeLoadHtml() {
	// $('.loadOrder').remove();
//	$('.loadOrder').hide();
	$('#loadRefresh').hide();
	$('#loadNex').hide();
}

function loadNewSlides() {
	// loadOrderByPage();
	setTimeout(function() {
		// Prepend new slide
		// var colors = ['blue','red','green','orange','pink'];
		// var color = colors[Math.floor(Math.random()*colors.length)];
		// mySwiper.prependSlide('<div class="title">www.internetke.com
		// '+slideNumber+'</div>', 'swiper-slide '+color+'-slide');
		// Release interactions and set wrapper
		mySwiper.setWrapperTranslate(0, 0, 0);
		mySwiper.params.onlyExternal = false;
		// Update active slide
		mySwiper.updateActiveSlide(0)
		// Hide loader
		$('.preloader').removeClass('visible');
	}, 1000)
	slideNumber++;
}

function showOrder(json,place) {
	try {
		if (codeSuccess(json)) {
			var orderList = json['CUST_LIST'];
			var order = {};
			if(place == 0){
				for (var i = orderList.length - 1; i >= 0; i--) {
					order = orderList[i];
					addOrder(order,place);
				}
			}else{
				for (var i = 0; i < orderList.length; i++) {
					order = orderList[i];
					addOrder(order,place);
				}
			}
			if (!mySwiper) {
				onSwiper();
			}
			// if(orderList.length <= G_NumPerPag && orderList.length > 0){
			// page ++;
			// }
		}
	} catch (e) {
		// TODO: handle exception
	}
	// alert($(".orderListView").length);
}

function doOrderDate(date) {
	if (date) {
		return date.substring(5, 11);
	}
	return "";
}

function addOrder(order,place) {
	//不重复添加
	if($("div[oid='"+order['CUST_ID']+"']").length > 0){
		return;
	}
	
	var BUY_STATUS = order['BUY_STATUS'];
	var BUY_VAL = "普通商户";
	if(BUY_STATUS == '1'){
		BUY_VAL = "VIP商户";
	}
	
	var html = "";
	html += "<div class='col-sm-8 col-xs-12 is-click menu-option col-sm-offset-2 swiper-slide orderView' oid='"+order['CUST_ID']+"'>";
	html += '	<div class="ace-icon left">';
//	html += img;
	html += '	</div>';
	html += '	<div class="menu-body clear-padding">';
	html += '		<div class="col-xs-12 clear-padding">';
	html += '			<div class="left">';
	html += order['CUST_NAME'] + '&nbsp;&nbsp;';
	html += '			</div>';
	html += '			<div class="right">';
	html += '				<div class="left describe">' + BUY_VAL + '&nbsp;&nbsp;' + order['CUST_LOGIN'] + '</div>';
//	html += '				<img src="images/arrow.png" class="arrow" />';
	html += '			</div>';
	html += '		</div>';
	html += '	</div>';
	html += '</div>';
	//	
	// html += order['PRDNAME']+order['ORDERDATE'] ;
	// html += "</div>";
	// html += "<div class='right'><span class='arrow' >></span></div>"
	// html += "<div class='right'>"+order['ORDAMT']+"</div></div>"
	
	//在前面插入
	if(place == 0){
		getOrderListView().prepend(html);
	}else{//在后面追加
		getOrderListView().append(html);
	}
}

loadOrderByPage();

// 加载完成执行
$(document).ready(function() {
	// onSwiper();
	// loadOrderByPage();
	// onSwiper();
	// loadNexPageOrder();
	orderViewSrawing();
	// alert($('.swiper-container').height());

	// $('.orderView').live("click",function(){});
	
	$("#navbar").on("click",function(){//回到顶部
		try {
			mySwiper.setWrapperTranslate(0, 0, 0);
		} catch (e) {
			// TODO: handle exception
		}
	});
});
var page = 1;
var G_NumPerPag = 19;

function loadOrderByPage() {
	// alert("进入加载");
	var data = {};

	data['PageNum'] = page;
	data['BUY_TYPE'] = BUY_TYPE;
	// alert(page+STARTDATE+ENDDATE);
	loadOrderList(data);
}

function orderViewSrawing() {
	var title_height = 70;
	var min_height = 0;
	var BG_WIDTH = document.documentElement.clientHeight;
	var $swiperContainer = $('.swiper-container');
	// alert(BG_WIDTH);
	// alert($swiperContainer.height());
	BG_WIDTH -= title_height;
	if (BG_WIDTH < min_height) {
		BG_WIDTH = min_height;
	}
	// alert(BG_WIDTH);

	$swiperContainer.css({
		"height" : BG_WIDTH + 'px'
	});

	// var $box = $('.dial-box');
	// var bodyH = $box.parent().height();
	// alert(BG_WIDTH - bodyH);
}

/**
 * 浏览器大小改变，对话框位置随之变化
 */
$(window).resize(function() {
	orderViewSrawing();
});
